package com.ghn.cocknovel.ui.activity.work

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.example.basemodel.base.BaseHeadVMActivity
import com.example.basemodel.base.DefaultHeadStyle
import com.ghn.cocknovel.R
import com.ghn.cocknovel.databinding.ActivityWorkTasksBinding
import com.ghn.cocknovel.ext.GONE
import com.ghn.cocknovel.ext.VISIBLE
import com.ghn.cocknovel.ext.pageLoadBase
import com.ghn.cocknovel.ui.activity.map.MapActivity
import com.ghn.cocknovel.ui.activity.myorder.MyorderActivity
import com.ghn.cocknovel.ui.activity.work.vm.WorkTasksViewModel
import com.ghn.cocknovel.utils.MDateUtils
import com.kt.NetworkModel.bean.ToDayWorkTaskBean
import java.text.SimpleDateFormat
import java.util.*

/**
 *@author: Administrator
 *2023/10/15
 *desc:排班计划
 */
class WorkTasksActivity : BaseHeadVMActivity<WorkTasksViewModel, ActivityWorkTasksBinding, DefaultHeadStyle>() {

    val dayOfWeek = arrayOf("周日", "周一", "周二", "周三", "周四", "周五", "周六")
    var mTopDataList: ArrayList<HashMap<String, String>> = ArrayList()
    val mTopTimeAdapter by lazy { TopTimeAdapter() }
    val mWorkAdapter by lazy { WorkAdapter() }

    override fun layoutId(): Int = R.layout.activity_work_tasks

    override fun initView(savedInstanceState: Bundle?) {
        setHeadTitle("排班计划")
        mDatabind.btnClose.setOnClickListener {
            mDatabind.llSelectData.GONE()
            mDatabind.llTopTimeList.VISIBLE()
            mViewModel.driverQueryTransitSchedulePlanListNet()
        }
        mDatabind.btnCalendar.setOnClickListener {
            startActivityForResult(Intent(this, WorkCalendarActivity::class.java), 100)
        }
        mTopTimeAdapter.bindToRecyclerView(mDatabind.mTopList)
        mWorkAdapter.bindToRecyclerView(mDatabind.mList)
        getWeeks()
        mViewModel.driverQueryTransitSchedulePlanListNet()
    }

    override fun createObserver() {
        mViewModel.mToDayWorkTaskBeanLiveData.observe(this) {
            pageLoadBase(it, mWorkAdapter)
            mWorkAdapter.setEmptyView(R.layout.empty_content)
        }
    }

    private fun getWeeks() {
        val calendar = Calendar.getInstance()
        calendar.add(Calendar.DATE, 0)
        for (item in 0..6) {
            val map: HashMap<String, String> = HashMap()

            //设置当前日期偏移
            if (item > 0) {
                calendar.add(Calendar.DATE, 1)
            }
            val year = calendar.get(Calendar.YEAR)
            //月
            val month = calendar.get(Calendar.MONTH) + 1
            //日
            val day = calendar.get(Calendar.DAY_OF_MONTH)
            val DAY_OF_WEEK = calendar.get(Calendar.DAY_OF_WEEK)
            val week = dayOfWeek[DAY_OF_WEEK - 1]
            map.put("$year-$month-$day", week)
            mTopDataList.add(map)
        }
        mTopTimeAdapter.setNewData(mTopDataList)
    }

    inner class TopTimeAdapter : BaseQuickAdapter<HashMap<String, String>, BaseViewHolder>, BaseQuickAdapter.OnItemClickListener {
        var clickPosition = 0

        constructor() : super(R.layout.item_top_time, null) {
            onItemClickListener = this
        }

        override fun convert(helper: BaseViewHolder, item: HashMap<String, String>) {
            helper.setText(R.id.tvTime, SimpleDateFormat("MM/dd").format(SimpleDateFormat("yyyy-MM-dd").parse(item.keys.toList()[0])))
            helper.setText(R.id.tvWeek, item.values.toList()[0])
            helper.setBackgroundColor(R.id.llRoot, if (helper.layoutPosition == clickPosition) getColor(R.color.color_base_blue) else getColor(R.color.white))
            helper.setTextColor(R.id.tvTime, if (helper.layoutPosition == clickPosition) getColor(R.color.white) else getColor(R.color.black))
            helper.setTextColor(R.id.tvWeek, if (helper.layoutPosition == clickPosition) getColor(R.color.white) else getColor(R.color.black))
        }

        override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View?, position: Int) {
            clickPosition = position
            notifyDataSetChanged()
            mViewModel.driverQueryTransitSchedulePlanListNet((adapter.getItem(position) as HashMap<String, String>).keys.toList()[0])
        }

    }

    inner class WorkAdapter : BaseQuickAdapter<ToDayWorkTaskBean, BaseViewHolder>, BaseQuickAdapter.OnItemChildClickListener {
        constructor() : super(R.layout.item_to_day_work_task, null) {
            onItemChildClickListener = this
        }

        override fun convert(helper: BaseViewHolder, item: ToDayWorkTaskBean) {
            helper.setText(R.id.tvTitle, item.fenceAreaName)
            helper.setText(R.id.tvState, getStateName(item.taskState))
            helper.setText(R.id.tvAddress, item.handoverLocation)
            helper.setText(R.id.tvTime, item.taskStartTime + " - " + item.taskEndTime)
            helper.setText(R.id.tvCarNum, item.carNumber)
            helper.getView<View>(R.id.btnStartTask).GONE()
            helper.getView<View>(R.id.btnWorkDetail).GONE()
            helper.getView<View>(R.id.btnOrderDetail).GONE()
            when (item.taskState) {
                3 -> {
                    helper.setText(R.id.btnStartTask, "查看任务")
                    helper.getView<View>(R.id.btnStartTask).VISIBLE()
                }
                4 -> {
                    helper.getView<View>(R.id.btnWorkDetail).VISIBLE()
                    helper.getView<View>(R.id.btnOrderDetail).VISIBLE()
                }
                else -> {
                    helper.getView<View>(R.id.btnStartTask).GONE()
                }
            }
            helper.addOnClickListener(R.id.btnStartTask)
            helper.addOnClickListener(R.id.btnWorkDetail)
            helper.addOnClickListener(R.id.btnOrderDetail)
        }

        override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
            val toDayWorkTaskBean = adapter.getItem(position) as ToDayWorkTaskBean
            when (view.id) {
                R.id.btnStartTask -> {
                    when (toDayWorkTaskBean.taskState) {
                        3 -> {
                            // 跳转到地图
                            startActivity(
                                Intent(this@WorkTasksActivity, MapActivity::class.java)
                                    .putExtra("taskId", toDayWorkTaskBean.id)
                                    .putExtra("fenceAreaId", toDayWorkTaskBean.fenceAreaId)
                            )
                        }
                    }
                }
                R.id.btnWorkDetail -> {
                    //  查看任务详情
                    startActivity(Intent(this@WorkTasksActivity, TaskDetailsActivity::class.java).putExtra("id", toDayWorkTaskBean.id))
                }
                R.id.btnOrderDetail -> {
                    //   查看订单详情
                    startActivity(
                        Intent(this@WorkTasksActivity, MyorderActivity::class.java)
                            .putExtra("taskId", toDayWorkTaskBean.id)
                    )

                }
            }
            val workTaskBean = adapter.getItem(position) as ToDayWorkTaskBean
            //TODO 传入经纬度
//            mViewModel.confirmTransitSchedulePlanNet(workTaskBean.id.toString(),)
        }

        fun getStateName(state: Int): String {
            //'任务状态 1未开始 2待确认 3进行中 4已结束 5已关闭
            when (state) {
                1 -> return "未开始"
                2 -> return "待确认"
                3 -> return "进行中"
                4 -> return "已结束"
                5 -> return "已关闭"
                else -> {
                    return ""
                }
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            when (requestCode) {
                100 -> {
                    val selectTime = data?.getStringExtra("selectTime").toString()
                    mViewModel.driverQueryTransitSchedulePlanListNet(selectTime)
                    mDatabind.llSelectData.VISIBLE()
                    mDatabind.llTopTimeList.GONE()
                    mDatabind.tvSelectTime.text = selectTime
                }
            }

        }
    }


}